﻿Imports System.Data.SqlClient

Public Class frmCategoryAct

    Private Sub frmCategoryAct_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Public Sub bindtypetem()
        cbtypeid.Items.Clear()
        cbtypeid.DisplayMember = "TypeName"
        cbtypeid.ValueMember = "TypeId"
        cbtypeid.DataSource = dbconnection.ExecuteDatatable("select Distinct TypeId, TypeName from Type where Active=1")
        cbtypeid.SelectedIndex = -1
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Try
            Me.Close()
            With frmCategory
                .bindData("")
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmCategory)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString())
        End Try
    End Sub
    Private Shared Function validcate(ByVal catename As String, ByVal typeid As String, ByVal cateid As String)
        Try
            Dim catehave = dbconnection.ExecuteScalar("select * from Category where CategoryName=N'" + catename + "' and TypeId='" + typeid + "' and CategoryId<>'" + cateid + "';")
            Return catehave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (validcate(txtCategory.Text, cbtypeid.SelectedValue, lblCategoryId.Text) IsNot Nothing) Then
            MessageBox.Show("Errors: Product Category Already Exists Please Try With Other...!")
            txtCategory.Focus()
        Else
            If (txtCategory.Text = "" Or cbtypeid.SelectedIndex = -1) Then
                MessageBox.Show("Errors: Please complete all field to continues...!")
                txtCategory.Focus()
            Else
                If (lblCategoryId.Text = "") Then
                    Try
                        Dim cmdadd As SqlCommand
                        cmdadd = dbconnection.Connection.CreateCommand
                        With cmdadd
                            .CommandType = CommandType.Text
                            .CommandText = "insert into Category (CategoryName, TypeId, Active) values (@CategoryName, @TypeId, '1');"
                            .Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value = txtCategory.Text.Trim()
                            .Parameters.Add("@TypeId", SqlDbType.NVarChar).Value = cbtypeid.SelectedValue
                            .ExecuteNonQuery()
                        End With
                        MessageBox.Show("Record Added")
                        Me.Close()
                        With frmCategory
                            .bindData("")
                            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                            .TopLevel = False
                            .Dock = DockStyle.Fill
                            frmMain.pnBody.Controls.Add(frmCategory)
                            .Show()
                        End With
                    Catch ex As Exception
                        MessageBox.Show("Errors " + ex.ToString)
                    End Try
                Else
                    Try
                        Dim cmdadd As SqlCommand
                        cmdadd = dbconnection.Connection.CreateCommand
                        With cmdadd
                            .CommandType = CommandType.Text
                            .CommandText = "update Category set CategoryName=@CategoryName, TypeId=@TypeId where CategoryId=@CategoryId;"
                            .Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value = txtCategory.Text.Trim()
                            .Parameters.Add("@TypeId", SqlDbType.NVarChar).Value = cbtypeid.SelectedValue
                            .Parameters.Add("@CategoryId", SqlDbType.NVarChar).Value = lblCategoryId.Text.Trim()
                            .ExecuteNonQuery()
                        End With
                        MessageBox.Show("Record Updated")
                        Me.Close()
                        With frmCategory
                            .bindData("")
                            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                            .TopLevel = False
                            .Dock = DockStyle.Fill
                            frmMain.pnBody.Controls.Add(frmCategory)
                            .Show()
                        End With
                    Catch ex As Exception
                        MessageBox.Show("Errors " + ex.ToString)
                    End Try
                End If
            End If
        End If
    End Sub
End Class